import Const from './const';


const PromiseUtils = {
    async flattenValue(objOrFunctionOrPromise, params, maxCount = Const.MaxFlattenValueDepth) {
        let e = objOrFunctionOrPromise;
        while (maxCount-- > 0) {
            if (typeof e === 'function') {
                if (params instanceof Array) {
                    e = e(...params);
                } else {
                    e = e(params);
                }
            } else if (e instanceof Promise) {
                e = await e;
            } else {
                return e;
            }
        }
    },
};


export function Syncoronized(target, name, desc) {
    console.log(target, name, desc);
    let func = desc.value;
    let lock = 0;
    let delegate = async function() {
        let ret = await func.apply(this, arguments);
        return ret;
    };
    desc.value = delegate;
    delete target[name];
    Object.defineProperty(target, name, desc);
}

export default PromiseUtils;